package com.pithypan.controller;

import com.pithypan.common.Constants;
import com.pithypan.common.FileDelFlagEnums;
import com.pithypan.common.utils.R;
import com.pithypan.common.utils.StringTools;
import com.pithypan.dto.SessionWebUserDto;
import com.pithypan.mapper.FileInfoMapper;
import com.pithypan.service.FileInfoService;
import com.pithypan.vo.FileInfoQuery;
import com.pithypan.vo.PaginationResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 回收站模块
 */
@RestController
@RequestMapping("/recycle")
public class RecycleController {

    @Resource
    private FileInfoService fileInfoService;


    /**
     * 回收站列表分页查询
     * @param session
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/loadRecycleList")
    public R loadRecycleList(HttpSession session, Integer pageNo, Integer pageSize) {
        //从session中获取登陆用户信息
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);

        FileInfoQuery query = new FileInfoQuery();
        query.setPageNo(pageNo);
        query.setPageSize(pageSize);
        query.setUserId(webUserDto.getUserId());
        query.setDelFlag(FileDelFlagEnums.RECYCLE.getFlag());
        query.setOrderBy("recovery_time desc");
        PaginationResultVo result = fileInfoService.findListByPage(query);

        return R.ok().data("recycleList", result);
    }


    /**
     * 从回收站还原文件
     * @param session
     * @param fileIds
     * @return
     */
    @RequestMapping("/recoverFile")
    public R recoverFile(HttpSession session, String fileIds) {
        //从session中获取登陆用户信息
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        fileInfoService.recoverFileBatch(webUserDto.getUserId(), fileIds);
        return R.ok();
    }


    /**
     * 文件彻底删除（从数据库中删除）
     * @param session
     * @param fileIds   fileId,fileId...
     * @return
     */
    @RequestMapping("/delFile")
    public R delFile(HttpSession session, String fileIds) {
        //从session中获取登陆用户信息
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        fileInfoService.delFileBatch(webUserDto.getUserId(), fileIds, false);
        return R.ok();
    }

}
